package com.kfm.hotel.service.impl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.kfm.hotel.domain.Room;
import com.kfm.hotel.domain.RoomType;
import com.kfm.hotel.domain.SysUser;
import com.kfm.hotel.service.RoomTypeService;
import com.kfm.hotel.mapper.RoomTypeMapper;
import com.kfm.hotel.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
* @author Lenovo
* @description 针对表【room_type】的数据库操作Service实现
* @createDate 2023-06-12 10:23:37
*/
@Service
public class RoomTypeServiceImpl extends MPJBaseServiceImpl<RoomTypeMapper, RoomType>
    implements RoomTypeService {
    /**
     * 分页功能
     *
     * @param roomType
     * @param page
     * @param limit
     * @return
     */
    //4.从service接口中调到接口类中
    @Override
    public PageInfo<RoomType> list(RoomType roomType, Integer page, Integer limit) {
        //分页
        //查询条件
        QueryWrapper<RoomType> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(roomType.getType())){
            queryWrapper.like("type", roomType.getType());
        }
        //如果房间类型不为空，查最小值
        if (!ObjectUtils.isEmpty(roomType.getPriceMin())){
            // 价格是以分为单位的，所以要乘以100. ge:>=,gt:>,le:<=
            queryWrapper.ge("price", roomType.getPriceMin() * 100);
        }
//如果房间类型不为空，查最大值
        if (!ObjectUtils.isEmpty(roomType.getPriceMax())){
            // 价格是以分为单位的，所以要乘以100. 小于等于
            queryWrapper.le("price", roomType.getPriceMax() * 100);
        }
        queryWrapper.eq("state",1);
        queryWrapper.eq("is_del",0);

        //查询分装成pageinfo对象
        PageHelper.startPage(page,limit);
//        List<RoomType> roomTypes01 = getBaseMapper().selectList(queryWrapper);
        //listDeep:深度查询
       // List<RoomType> roomTypes01 = listDeep(queryWrapper);
        List<RoomType> rooms = listDeep(queryWrapper, conf -> conf.loop(true));
        PageInfo<RoomType> pageInfo = new PageInfo<>(rooms);

        return pageInfo;
    }

    @Override
    public List<RoomType> getRoomTypes() {
        QueryWrapper<RoomType> wrapper = new QueryWrapper<>();
        wrapper.eq("state",1);
        wrapper.eq("is_del",0);
        return list(wrapper);
    }


//    @Override
//    public int saveRoomType(RoomType room) {
//        //判断用户名是否存在,查询count即可
//        QueryWrapper wrapper = new QueryWrapper<>();
//        wrapper.select("count(1) as num");
//        //姓名是否相等
//        wrapper.eq("type", room.getType());
//        //是否被前端删除
//        wrapper.eq("is_del", 0);
//        //是否禁用状态
//        wrapper.eq("state", 1);
//
//        Map<String, Long> map = getMap(wrapper);
//        if (map.get("num") > 0) {
//            throw new RuntimeException("该房间类型已经存在");
//        }
//    return save(room) ? 1 : -1;
//    }

}









